﻿@page "/creator/plugins/new"
@page "/creator/plugins/{id}"
@inherits BaseComponent

<UserLayout Title="编辑插件">
    <CmsCard Class="cl-creator-form" Title="编辑插件">
        @if (!string.IsNullOrWhiteSpace(errorMessage))
        {
            <AntAlert Type="error">@errorMessage</AntAlert>
        }
        <AntForm Id="@formId">
            <div class="cl-row">
                <CmsFormItem Label="名称" Required>
                    <AntText Id="@nameof(CmPlugin.Name)" Value="@model?.Name" Placeholder="请输入名称" Required />
                </CmsFormItem>
                <CmsFormItem Label="作者" Required>
                    <AntText Id="@nameof(CmPlugin.Author)" Value="@model?.Author" Placeholder="请输入作者" Required />
                </CmsFormItem>
            </div>
            <CmsFormItem Label="摘要" Required>
                <AntText Id="@nameof(CmPlugin.Summary)" Value="@model?.Summary" Placeholder="请输入摘要" Required />
            </CmsFormItem>
            <CmsFormItem Label="分类" Required>
                <AntRadio Id="@nameof(CmPlugin.Category)" Value="@model?.Category" Items="CodeTable.PluginCategories" />
            </CmsFormItem>
            <div class="cl-row">
                <CmsFormItem Label="类型" Required>
                    <AntRadio Id="@nameof(CmPlugin.Type)" Value="@model?.Type" Items="CodeTable.PluginTypes" />
                </CmsFormItem>
                <CmsFormItem Label="价格">
                    <AntText Id="@nameof(CmPlugin.Price)" Value="@model?.Price" Placeholder="请输入价格" Required />
                </CmsFormItem>
            </div>
            <CmsFormItem Label="请详细描述插件内容和使用方法" Required>
                <CmsMarkdown Id="@nameof(CmPlugin.Description)" Value="@model?.Description" Placeholder="在这里输入内容" />
            </CmsFormItem>
            <div class="button">
                @* <AntButton Name="暂存" OnClick="@($"return AntBlazor.validate('{formId}');")" /> *@
                <AntButton Name="发布" OnClick="@($"return AntBlazor.validate('{formId}');")" />
                <AntButton Name="取消" Type="default" OnClick="@($"location='{Url.UserPlugins}';return false;")" />
            </div>
        </AntForm>
    </CmsCard>
</UserLayout>

@code {
    private CmPlugin model = new();
    private string formId = "post-form";
    private string errorMessage = "";
    private IPluginService Service;

    protected override async Task OnInitAsync()
    {
        if (CurrentUser == null)
            return;

        await base.OnInitAsync();
        Service = await CreateServiceAsync<IPluginService>();

        model.Author = CurrentUser.Name;
        if (!string.IsNullOrWhiteSpace(Id))
            model = await Service.GetPluginAsync(Id);
        if (Context.Request.IsHandler(formId))
        {
            model = Context.Request.GetModel<CmPlugin>();
            if (!string.IsNullOrWhiteSpace(Id))
                model.Id = Id;
            var result = await Service.SavePluginAsync(model);
            if (!result.IsValid)
                errorMessage = result.Message;
            else
                Context.Response.Redirect(Url.UserPlugins.Trim('.'));
        }
    }
}